package com.example.lj.uav.utils;

import android.content.Context;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
import android.support.annotation.Nullable;

public class MyDBHelper extends SQLiteOpenHelper {
    private static final int DB_VERSION = 1;
    private static final String DB_NAME = "UAV.db";
    public static final String TABLE_ROUTE = "Route";
    public static final String TABLE_POINT = "Point";
    public static final String TABLE_POINT_ACTION = "PointAction";
    public static final String TABLE_CAMERA = "Camera";

    public MyDBHelper(@Nullable Context context) {
        super(context, DB_NAME, null, DB_VERSION);
    }


    @Override
    public void onCreate(SQLiteDatabase sqLiteDatabase) {
        String sql = "create table if not exists " + TABLE_ROUTE + " (" +
                "Id integer primary key AUTOINCREMENT," +
                "FileName varchar(50)," +
                "altitude double," +
                "autoFlightSpeed double," +
                "actionOnFinish text," +
                "headingMode text," +
                "gimbalPitchMode text," +
                "altitudeMode text," +
                "coordinates text,"+
                "updateTime long,"+
                "updatePlace text,"+
                "collected Integer,"+
                "type text,"+
                "image_path text,"+
                "stopWhenLost Integer"+
                ")";
        sqLiteDatabase.execSQL(sql);

        String sqlPoint = "create table if not exists " + TABLE_POINT + "(Id integer,"+
                "latitude double,"+
                "longitude double,"+
                "altitude double,"+
                "altitudeMode text,"+
                "direction text,"+
                "angle double,"+
                "pitchAngle double,"+
                "motion text,"+
                "file_name varchar(50))";

        sqLiteDatabase.execSQL(sqlPoint);

        String sqlPointMotion = "create table if not exists "+TABLE_POINT_ACTION+"(Id integer," +
                "point_id integer," +
                "point_action text," +
                "param text," +
                "file_name varchar(50)" +
                ")";
        sqLiteDatabase.execSQL(sqlPointMotion);

        String sqlCamera = "create table if not exists "+TABLE_CAMERA+"(name text,sensorW text,sensorH text,focal_length text)";
        sqLiteDatabase.execSQL(sqlCamera);
    }

    @Override
    public void onUpgrade(SQLiteDatabase sqLiteDatabase, int i, int i1) {
        String sql = "DROP TABLE IF EXISTS " + TABLE_ROUTE;
        sqLiteDatabase.execSQL(sql);
        String sql2 = "DROP TABLE IF EXISTS " + TABLE_POINT;
        sqLiteDatabase.execSQL(sql2);
        String sql3 = "DROP TABLE IF EXISTS " +TABLE_POINT_ACTION;
        sqLiteDatabase.execSQL(sql3);
        String sql4 = "DROP TABLE IF EXISTS " +TABLE_CAMERA;
        sqLiteDatabase.execSQL(sql4);
        onCreate(sqLiteDatabase);
    }
}
